//
//  NSObject+MLKitDAO.m
//  Pods
//
//  Created by molon on 15/7/21.
//
//

#import "NSObject+MLKitDAO.h"
#import "Model.h"
#import "ModelArray.h"
#import "MLKitDebug.h"
#import "MLDataHelper.h"

@implementation NSObject (MLKitDAO)

- (void)setValue:(id)aObject forKey:(NSString*)key andKeyType:(NSString*)type
{
    //    if (!aObject||[aObject isKindOfClass:[NSNull class]]) {
    //虽然为空，但是可以往下运行
    //        DLOG(@"属性%@给予的值为空",key);
    //    }
    
    if ([type isEqualToString:@"T#"]) {
        DLOG(@"属性%@的类型%@不在动态绑定处理范围内,%@只可处理标量以及NSString,NSDictionary,NSArray,NSURL,Model子类,ModelArray子类",key,type,NSStringFromSelector(_cmd));
        return;
    }
    
    //检查是否是标量,是的话对其赋值
    if (![type hasPrefix:@"T@\""]||![type hasSuffix:@"\""]) {
        //前缀和后缀，有其一都不可
        [self setValue:[MLDataHelper getNumberValue:aObject defaultValue:@(0)] forKey:key];
        return;
    }
    
    //到此type被认作是对象，NSObject，具有前缀和后缀
    type = [type substringWithRange:NSMakeRange(3, [type length]-4)];
    //检查是否是NSNumber
    if ([type isEqualToString:@"NSNumber"]) {
        //数据类型为NSNumber,简单赋值,由于NSNumber alloc init无意义，需要在此单独处理
        [self setValue:[MLDataHelper getNumberValue:aObject defaultValue:nil]
                forKey:key];
        return;
    }else if ([type isEqualToString:@"NSURL"]) {
        //原因同上
        [self setValue:[MLDataHelper getURLValue:aObject defaultValue:nil]
                forKey:key];
        return;
    }
    
    //到这里的说明都可以set nil
    if (!aObject||[aObject isKindOfClass:[NSNull class]]) {
        [self setValue:nil forKey:key];
        return;
    }
    
    Class cls = NSClassFromString(type);
    //数据类型为NSString,NSDictionary,NSArray但非ModelArray的子类,NSURL则简单赋值
    if ([cls isSubclassOfClass:[NSString class]]){
        [self setValue:[MLDataHelper getStringValue:aObject defaultValue:nil]
                forKey:key];
    }else if([cls isSubclassOfClass:[NSDictionary class]]) {
        if ([aObject isKindOfClass:[NSDictionary class]]) {
            [self setValue:[MLDataHelper getDictionaryValue:aObject defaultValue:nil]
                    forKey:key];
        }else{
            DLOG(@"属性%@给予的值不是字典类型1，忽略它",key);
        }
    }else if([cls isSubclassOfClass:[NSArray class]]){
        if ([aObject isKindOfClass:[NSArray class]]) {
            [self setValue:[MLDataHelper getArrayValue:aObject defaultValue:nil]
                    forKey:key];
        }else{
            DLOG(@"属性%@给予的值不是数组类型1，忽略它",key);
        }
    }else if ([cls isSubclassOfClass:[Model class]]){
        //是Model的子类就直接调用其turnObject方法
        if ([aObject isKindOfClass:[NSDictionary class]]) {
            [self setValue:[cls turnObject:aObject] forKey:key];
        }else{
            DLOG(@"属性%@给予的值不是字典类型2，忽略它",key);
        }
    }else if ([cls isSubclassOfClass:[ModelArray class]]){
        if ([aObject isKindOfClass:[NSArray class]]) {
            //ModelArray的turnObject里遍历并且调用对应的Model turnObject
            [self setValue:[cls turnObject:aObject] forKey:key];
        }else{
            DLOG(@"属性%@给予的值不是数组类型2，忽略它",key);
        }
    }else{
        DLOG(@"属性%@的类型%@不在动态绑定处理范围内,%@只可处理标量以及NSString,NSDictionary,NSArray,NSURL,Model子类,ModelArray子类",key,type,NSStringFromSelector(_cmd));
    }
    
}

@end
